<?php
// +----------------------------------------------------------------------
// | Author: heizai <876555425@qq.com>
// +----------------------------------------------------------------------
namespace app\api\controller;

use cmf\controller\AdminBaseController;
use app\api\model\PluginMessageModel;
use think\Db;
use app\admin\model\ThemeModel;

class AdminIndexController extends AdminBaseController
{

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    /**
     * Message type
     * @adminMenu(
     *     'name'   => 'Message type',
     *     'parent' => 'api/AdminIndex/index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'Message type',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $model = new PluginMessageModel();
        $datas = $model->order('id desc')->paginate(10);
        $this->assign("datas", $datas);
        $this->assign('page', $datas->render());
        return $this->fetch();
    }

    /**
     * Message add
     * @adminMenu(
     *     'name'   => 'Message add',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'Message add',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        // die('4545');
        return $this->fetch();
    }

    /**
     * Message addPost
     * @adminMenu(
     *     'name'   => 'Message addPost',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'Message addPost',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $model = new PluginMessageModel();
        $data = $this->request->post();
        $result = $this->validate($data['post'], "Post");

        if ($result !== true) {
            $this->error($result);
        }
        if ($this->request->isPost()) {
            $data['post']['createtime'] = date('Y-m-d H:i:s');
            $result=$model->allowField(true)->save($data['post']);
            if ($result!==false) {
                $this->success("Successful message!");
            } else {
                $this->error("The message failed!");
            }
        }
    }


    /**
     * Message edit
     * @adminMenu(
     *     'name'   => 'Message edit',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'Message edit',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        

        $id = $this->request->param('id',0,'intval');
        
        $model = new PluginMessageModel();

        $datas = $model->where(array('id'=>$id))->find();

        $this->assign('post',$datas);

        return $this->fetch();
    }


    /**
     * Message editPost
     * @adminMenu(
     *     'name'   => 'Message editPost',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'Message editPost',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        
        $model = new PluginMessageModel();
        $data = $this->request->post();
        $result = $this->validate($data['post'], "Post");
        if ($result !== true) {
            $this->error($result);
        }
        if ($this->request->isPost()) {
            // print_r($model->allowField(true)->data($data['post'], true)->save());die;
            // $model->allowField(true)->data($data['post'], true)->where(array('id'=>$data['id']))->save();
            $result = Db::name('guestbook_cat')->where(array('id'=>$data['id']))->data($data['post'], true)->update();
            if ($result!==false) {
                $this->success("Successful message!");
            } else {
                $this->error("The message failed!");
            }
        }
    }


}
